/* *********
 *    Author: Jason He;
 *    Version: 0.0.1;
 *    Date: 2016-12-20;
 *    File: detail.js;
 ********* */

$(document).ready(function () {
	// summary slide
	var summary = new Swiper('#summary-slide', {
		simulateTouch: false,
		onSlideChangeStart: function (swiper) {
			$("#summary-num").text(swiper.activeIndex + 1);
			thumb.swipeTo(swiper.activeIndex, 300, false);
			$(".thumb-slide-border").removeClass("cur");
			$("#thumb-slide").find(".swiper-slide").eq(swiper.activeIndex).find(".thumb-slide-border").addClass("cur");
		}
	});
	$("#summary-btn-left").click(function () {
		summary.swipePrev();
	});
	$("#summary-btn-right").click(function () {
		summary.swipeNext();
	});
	var thumb = new Swiper('#thumb-slide', {
		simulateTouch: false,
		slidesPerView: 6,
		slidesPerGroup: 6
	});
	$("#thumb-btn-left").click(function () {
		thumb.swipePrev();
	});
	$("#thumb-btn-right").click(function () {
		thumb.swipeNext();
	});
	$(".thumb-slide-img").mouseover(function () {
		$(".thumb-slide-border").removeClass("cur");
		$(this).prev().addClass("cur");
		var index = $(this).parent().index();
		summary.swipeTo(index, 300, false);
		$("#summary-num").text(index + 1);
	});

	// summary feedback
	$("#summary-source").click(function () {
		$("#feedback").show();
	});

	// summary stipul
	$("#summary-btn-stipul").click(function () {
		$("#stipul").show();
	});
	$("#stipul-close").click(function () {
		$("#stipul").hide();
		$("#stipul-phone").val("");
		$("#stipul-error").text("");
	});
	$("#stipul-submit").click(function () {
		var phone = $("#stipul-phone").val();
		if (phone) {
			var reg = new RegExp(/^(1[34578]\d{9})|^(0\d{2,3}-?\d{7,8})$/);
			if (reg.test(phone)) {
				$.ajax({
				  type: 'POST',
				  url: "/Home/Seller/addPhone",
				  data: {phone:$('#stipul-phone').val(),carid:$('#carid').val()},
				  dataType:'json',
				  success:function(res){console.log(res);
					  if(res.id==1)
					  {
						  alert(res.msg);
					  }else
					  {
						  alert('出现异常，请联系客服');
					  }
				  }
				});
				//alert("预约成功！我们会及时与您电话联系。");
				$("#stipul").hide();
				$("#stipul-phone").val("");
				$("#stipul-error").text("");
			} else {
				$("#stipul-error").text("请填写正确的手机号码哦~").show();
				$("#stipul-phone").focus();
				return false;
			}
		} else {
			$("#stipul-error").text("忘记填写手机号码了哦~").show();
			$("#stipul-phone").focus();
			return false;
		}
	});
	$("#bargain-send").click(function () {
		if (!$(this).hasClass("disabled")) {
			var phone = $("#bargain-phone").val();
			if (phone) {
				var reg = new RegExp(/^1[34578]\d{9}$/);
				if (reg.test(phone)) {
					/*/index.php?m=Web&c=Index&a=sendMsg&phone=13008113661*/
					$.ajax({
					  type: 'POST',
					  url: "/Home/Post/getCode",
					  data:{phone:phone},
					  dataType:'json',
					  success:function(res){
						  $('#flag').val(res.Data);
						  $(this).addClass("disabled").text("60秒").prev().text("验证码已发送~");
						  //alert();
					  }
					});
					
					var num = 60;
					var timer = setInterval(function () {
						num--;
						$("#bargain-send").text(num + "秒");
						if (num == 0) {
							clearInterval(timer);
							$("#bargain-send").removeClass("disabled").text("获取验证码");
						}
					}, 1000);
				} else {
					$(this).prev().text("请填写正确的手机号码哦~");
					$("#bargain-phone").focus();
					return false;
				}
			} else {
				$(this).prev().text("忘记填写手机号码了哦~");
				$("#bargain-phone").focus();
				return false;
			}
		}
	});
	$("#bargain-submit").click(function () {
		//alert("提交成功");
		var carid =$('#carid').val();
		var price =$('#bargain-price').val();
		var phone = $("#bargain-phone").val();
		var flag = $("#flag").val();
		var phonecode =$('#bargain-code').val();
		$.ajax({
			  type: 'POST',
			  url: "/Home/Seller/cutPrice",
			  data:{flag:flag,price:price,phone:phone,carid:carid,phonecode:phonecode},
			  dataType:'json',
			  success:function(res){console.log(res);
				  if(res.Ret==0)
				  {
					  alert('提交成功');
					  $("#bargain").hide();
					  $("#bargain-price").val("");
					  $("#bargain-phone").val("");
					  $("#bargain-code").val("");
					  $(".bargain-form-input-error").text("");
				  }else
				  {
					  alert(res.Msg);
				  }
				  //$(this).addClass("disabled").text("60秒").prev().text("验证码已发送~");
			  }
		});
		
	});

	// summary bargain
	$("#summary-btn-bargain").click(function () {
		$("#bargain").show();
	});
	$("#bargain-close").click(function () {
		$("#bargain").hide();
		$("#bargain-price").val("");
		$("#bargain-phone").val("");
		$("#bargain-code").val("");
		$(".bargain-form-input-error").text("");
	});

	// fixnavbar
	$("#fixnavbar-btn").click(function () {
		$("#stipul").show();
	});
	var fixnavbarTop = $("#fixnavbar").offset().top;
	var basicinfoTop = $("#basicinfo").offset().top - 50;
	var pictureTop = $("#picture").offset().top - 50;
	var configTop = $("#config").offset().top - 50;
	var scrollTop = 0;
	$(window).scroll(function () {
		scrollTop = $(window).scrollTop();
		if (scrollTop >= fixnavbarTop) {
			$("#fixnavbar").addClass("fixed");
		} else{
			$("#fixnavbar").removeClass("fixed");
		}
		if (scrollTop < pictureTop) {
			$(".fixnavbar-item").removeClass("cur");
			$(".fixnavbar-item").eq(0).addClass("cur");
		} else if (scrollTop >= pictureTop && scrollTop < configTop) {
			$(".fixnavbar-item").removeClass("cur");
			$(".fixnavbar-item").eq(1).addClass("cur");
		} else if (scrollTop >= configTop) {
			$(".fixnavbar-item").removeClass("cur");
			$(".fixnavbar-item").eq(2).addClass("cur");
		}
	});
	$("#fixnavbar-basicinfo").click(function () {
		$(document).scrollTop(basicinfoTop);
	});
	$("#fixnavbar-picture").click(function () {
		$(document).scrollTop(pictureTop);
	});
	$("#fixnavbar-config").click(function () {
		$(document).scrollTop(configTop);
	});
});